#ifndef WEATHERTO_H
#define WEATHERTO_H
#include<QMap>
#include<QString>
#include<QFile>
#include<QJsonObject>
#include<QJsonArray>
#include<QJsonDocument>
#include<QJsonValue>
#include<QMessageBox>
class weather
{
private:
    static QMap<QString,QString>mcitymap;
    static void initcitymap(){
        //读取文件
        QString filepath="C:\\Users\\20644\\Desktop\\city.json";
        QFile file(filepath);
        file.open(QIODevice::ReadOnly|QIODevice::Text);
        QByteArray json=file.readAll();
        file.close();
        qDebug()<<file.isOpen();
        //解析文件
        QJsonParseError err;
        QJsonDocument doc=QJsonDocument::fromJson(json,&err);
        if(err.error!=QJsonParseError::NoError)
        {
            return;
        }
        if(!doc.isArray())
        {
            return;
        }
        QJsonArray cityes=doc.array();
        for(int i=0;i<cityes.size();i++)
        {
         QString city=cityes[i].toObject().value("city_name").toString();
         QString code=cityes[i].toObject().value("city_code").toString();
         if(code.size()>0){
             mcitymap.insert(city,code);
         }
        }
    }
public:
    static QString getcitycode(QString cityname)
    {
        if(mcitymap.isEmpty())
        {
            initcitymap();
        }
    QMap<QString,QString>::iterator it= mcitymap.find(cityname);
        if(it==mcitymap.end())
        {
            it= mcitymap.find(cityname+"市");
        }
        if(it!=mcitymap.end())
        {
            return it.value();
        }
        return "";
    }
};
QMap<QString,QString>weather::mcitymap={};
#endif // WEATHERTO_H
